<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">系统日志</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!home_console">首页</a>
          <a><cite>系统日志</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            账号：
            <input id="log-edt-account" class="layui-input search-input" type="text" placeholder="请输入账号"/>&emsp;
            日期：
            <input id="log-edt-date" class="layui-input search-input date-icon" type="text" placeholder="请选择日期范围"
                   style="width: 195px;"/>&emsp;
            <button id="log-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
        </div>

        <table class="layui-table" id="log-table" lay-filter="log-table"></table>
    </div>
</div>

<!-- 表头操作列 -->
<script type="text/html" id="log-table-bar">
    <div class="layui-inline" lay-event="del" lay-tips="删除"><i class="layui-icon layui-icon-delete"></i></div>
</script>

<script>
    layui.use(['laydate', 'table', 'jquery'], function () {
        const $ = layui.jquery;
        const layDate = layui.laydate;
        const table = layui.table;

        // 渲染表格
        table.render({
            elem: '#log-table',
            url: 'monitor/log/list',
            toolbar: '#log-table-bar',
            defaultToolbar: ['filter'],
            page: true,
            cols: [
                [
                    {type: 'radio', fixed: 'left'},
                    {field: 'username', title: '账号'},
                    {field: 'url', title: '访问地址'},
                    {field: 'action', title: '操作动作'},
                    {field: 'args', title: '具体参数'},
                    {field: 'osName', title: '操作系统'},
                    {field: 'device', title: '设备'},
                    {field: 'browserType', title: '浏览器'},
                    {field: 'ip', title: 'IP'},
                    {field: 'createTime', title: '操作时间'},
                ]
            ]
        });

        // 时间范围
        layDate.render({
            elem: '#log-edt-date',
            type: 'date',
            range: true,
            theme: 'molv'
        });

        //头工具栏事件
        table.on('toolbar(log-table)', function (obj) {
            const checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
            const data = checkStatus.data;  //获取选中行数据

            switch (obj.event) {
                case 'del':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                        return;
                    }
                    del(data[0].id);
                    break;
            }

        });

        const del = function (id) {
            layer.confirm('确定要删除吗？', function () {
                layer.load(2);
                $.post(basePath + 'monitor/log/delete', {
                    logId: id
                }, function (data) {
                    layer.closeAll('loading');
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                        table.reload('log-table', {});
                    } else {
                        layer.msg(data.msg, {icon: 2});
                    }
                });
            });
        };

        // 搜索按钮点击事件
        $('#log-btn-search').click(function () {
            const searchDate = $('#log-edt-date').val().split(' - ');
            const searchUsername = $('#log-edt-account').val();
            table.reload('log-table', {
                where: {
                    startDate: searchDate[0],
                    endDate: searchDate[1],
                    username: searchUsername
                }
            });
        });
    });
</script>